package weeny.pre.display;

import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Logger;

import prefuse.data.expression.FunctionTable;
import prefuse.data.expression.parser.ExpressionParser;
import prefuse.data.parser.BooleanParser;
import prefuse.data.parser.ColorIntParser;
import prefuse.data.parser.DataParser;
import prefuse.data.parser.DateParser;
import prefuse.data.parser.DateTimeParser;
import prefuse.data.parser.DoubleArrayParser;
import prefuse.data.parser.DoubleParser;
import prefuse.data.parser.FloatArrayParser;
import prefuse.data.parser.FloatParser;
import prefuse.data.parser.IntArrayParser;
import prefuse.data.parser.IntParser;
import prefuse.data.parser.LongArrayParser;
import prefuse.data.parser.LongParser;
import prefuse.data.parser.ParserFactory;
import prefuse.data.parser.StringParser;
import prefuse.data.parser.TimeParser;
import prefuse.util.ui.UILib;
import weeny.pre.expression.AlphaFunction;
import weeny.pre.expression.NullPredicate;
import weeny.pre.io.parser.FontParser;
import weeny.pre.io.parser.Point2DParser;
import weeny.pre.io.parser.RectParser;
import weeny.pre.io.parser.StringArrayParser;
import weeny.pre.io.parser.StrokeParser;
import weeny.util.logs.LineFormatter;

/**
 * We are trying not to edit the source codes as much as possible
 * So if there is something we can hack, we hack here.
 * So this function should be called before every thing, although
 * it is not necessary to be actually before every thing.
 * 
 * In case we find some bugs or have to modify the prefuse source,
 * we mark them with "WEENY" magic word. Therefore in case necessary,
 * please search "WEENY" (case sensitive) to locate all the edited
 * parts in prefuse source codes. Another thing is whenever prefuse 
 * has a new release (which I seriously doubt~), you know where to 
 * change them again!
 */
public class Initialization {
	private static boolean inited = false;
	
	static {
		init();
	}
	public static boolean init(){
		if (inited) {//make sure it only run once
			return true;
		}
		else {
			inited = true;
		}
		
		//Change the parser log to remove the time stamp, which is annoying
		Logger log = Logger.getLogger(ExpressionParser.class.getName());
		log.setUseParentHandlers(false);
		for(Handler h : log.getHandlers()){
			log.removeHandler(h);
		}
		ConsoleHandler handler = new ConsoleHandler();
		handler.setFormatter(new LineFormatter());
		log.addHandler(handler);
		
		//add my functions to function table
		FunctionTable.addFunction(
				new NullPredicate().getName(), NullPredicate.class);
		FunctionTable.addFunction(
				new AlphaFunction().getName(), AlphaFunction.class);
		
		//add my own data parsers
		ParserFactory.DEFAULT_PARSERS =  
			new DataParser[] {
				new FontParser(),       //my own parser
				new StrokeParser(),     //my own parser
				new RectParser(),       //my own parser
				new Point2DParser(),    //my own parser
				new IntParser(),
				new LongParser(),
				new DoubleParser(),
				new FloatParser(),
				new BooleanParser(),
				new ColorIntParser(),
				new DateParser(),
				new TimeParser(),
				new DateTimeParser(),
				new IntArrayParser(),
				new LongArrayParser(),
				new FloatArrayParser(),
				new DoubleArrayParser(),
				new StringArrayParser(),//my own parser
				new StringParser()};
		
		ParserFactory.setDefaultFactory(
				new ParserFactory(ParserFactory.DEFAULT_PARSERS));
		UILib.setPlatformLookAndFeel();
		return true;
	}
}